﻿using System;
using System.Threading;
using PhiStore.ReturnCodes;
using PhiStore.Threading;

namespace PhiStore.ExampleCodes.PhiElection
{
    class ElectionResultReader:Boostable
    {
        private const string ElectionResultKey = "voting_result_2011";

        private PhiClient<ElectionResult> client;

        private object mutex = new object();

        private DateTime start;
        private ulong resultsread = 0L;
        private decimal throughput = 0;
        private bool stop = false;

        private ElectionResult result;
        public ElectionResult Result{
            get
            { 
                lock(mutex){
                    return result;
                }
            }
        }

        /// <summary>
        /// number of result reads pr. second
        /// </summary>
        public decimal ThroughPut
        {
            get
            {
                decimal result = 0;
                lock (mutex)
                {
                    result = throughput;
                }
                return result;
            }
        }

        /// <summary>
        /// Stop reading election results
        /// </summary>
        public void STOP()
        {
            lock (mutex)
            {
                stop = true;
            }
        }
      
        public ElectionResultReader(BroadcastObject bc)
        {
            client = new PhiClient<ElectionResult>(false, false);
            client.Initialize();
            client.Connect(bc.IP, bc.Port);
            bool GotInitialResult = false;
            while (!GotInitialResult)
            {
                try
                {
                    lock (mutex)
                    {
                        result = client.Read(ElectionResultKey);
                    }
                    GotInitialResult = true;
                }
                catch (PhiException)
                {
                    //not found
                    Thread.Sleep(1000);
                }
            }
            Booster.Boost(this);
        }

        public void Run()
        {
            

            start = DateTime.Now;
            //do stuff
            try
            {
                while (true)
                {
                    lock (mutex)
                    {
                        if (stop)
                        {
                            client.Disconnect();
                            break;
                        }
                    }

                    ElectionResult temp = new ElectionResult(null, 0L, 0L);
                    for (int i = 0; i < 1000; i++)
                    {
                        temp = client.Read(ElectionResultKey);
                    }

                    lock (mutex)
                    {
                        result = temp;
                        resultsread += 1000;
                        TimeSpan ts = DateTime.Now.Subtract(start);
                        throughput = Convert.ToDecimal(resultsread) / Convert.ToDecimal(ts.TotalSeconds);
                    }

                }
            }
            catch
            {
                //byebye
            }
        }
    }
}
